--取得授权验证网址


local httparg               = require("shared.api.httparg")
local response              = require("shared.api.response")
local config                = require("config")
local tag                   = httparg.tag()
local assertion             = httparg.assertion
local httpclient           = require "shared.http.client"
local util                  = require "proxy.game.common.util"
local ngx                   = _G.ngx
local get_response_spec     = util.get_response_spec
local fill_response_header  = util.fill_response_header
local is_match_content_type = util.is_match_content_type
local get_content_decoder   = util.get_content_decoder
local get_method            = util.get_method
local def                   = require "shared.api.def"


local cjson = require "cjson.safe"
      cjson.encode_max_depth(32)
      cjson.decode_max_depth(32)

local gameRgsGateWayUrl = config.ENV.GAMEGATERGS_SERVICE

local ARGS = {
  tag.body('json', 'required'),

  wallet_mechanism   = tag.json.wallet_mechanism   ('string', 'required'),
  game               = tag.json.game               ('string', 'required'),
  platform_currency  = tag.json.platform_currency  ('string', 'required'),
  currency           = tag.json.currency           ('string', 'required'),
  language           = tag.json.language           ('string', 'required'),
  account_type       = tag.json.account_type       ('string', 'required'),
  account            = tag.json.account            ('string', 'required'),
  player             = tag.json.player             ('string', 'required'),
  player_ip          = tag.json.player_ip          ('string', 'required'),
  fun                = tag.json.fun                ('boolean', 'required'),
}


  local referencer_account  = ""
  local referencer_username = ""
  local location            = config.ENV.DATAGATE_SERVICE .. "/account"
  local method              = "PEEK"
  local headers             = ngx.req.get_headers()
  local query


--移除方法设定
  local rmvLowKey = "x-http-method-override"
--移除gzip设定
  local rmvKey2   = "accept-encoding"
  for k,v in pairs(headers) do
      local low=string.lower(k)
  if low==rmvLowKey  or low==rmvKey2   then
    headers[k]= nil
  end
  end

  local accountReq={
    account = ARGS.account
  }

do

  local  requestData = cjson.encode(accountReq)
  local  resp, err   = httpclient.new()
                                :uri(location)
                                :headers(headers)
                                :query(query)
                                :body(requestData)
                                :send(method, 2000, nil)

  if err or (not resp) then
    ngx.status = 500
    ngx.say(err)
    ngx.exit(ngx.OK)
  end

  if not resp.body
          or not resp.headers
          or resp.status ~= 200
          or not is_match_content_type(resp, 'application/json')  then

    ngx.status = resp.status
    fill_response_header(resp.headers)
    ngx.say (resp.body)
    ngx.exit(ngx.OK)
  end

  local respBody
  local contentDecoder = get_content_decoder(resp)
  if contentDecoder then
     respBody = contentDecoder.decode(resp.body)
  else
    -- 这儿理论上不应该出错
    ngx.status = 500
    ngx.say(err)
    ngx.exit(ngx.OK)
  end


  referencer_account  = respBody.referencer_account
  referencer_username = respBody.referencer_username

end

  ngx.log(ngx.ERR,referencer_username)
  ngx.log(ngx.ERR,referencer_account)






--向GameGateway_Rgs 请求数据
local apiReq = {
  wallet_mechanism    = ARGS.wallet_mechanism,
  game                = ARGS.game,
  platform_currency   = ARGS.platform_currency,
  currency            = ARGS.currency,
  account_type        = ARGS.account_type,
  language            = ARGS.language,
  account             = ARGS.account,
  player              = ARGS.player,
  player_ip           = ARGS.player_ip,
  fun                 = ARGS.fun,
  referencer_account  = referencer_account ,
  referencer_username = referencer_username,
  game_rebate_rate    = "0",

}

--call GameGateway_Rgs

--根ulr
local ADDRESS = gameRgsGateWayUrl
--后面接的path
local ACTION  = string.format("%s/game/url",ngx.var.oauth_brand)
ngx.log(ngx.ERR,ACTION)
if not ADDRESS or not ACTION then
  ngx.status = ngx.HTTP_NOT_FOUND
  ngx.exit(ngx.OK)  
end

--从rgs取实际的url
do
  location = ngx.unescape_uri(ADDRESS..ACTION)
  method   = "PEEK"
  headers  = ngx.req.get_headers()
  ngx.req.read_body()
  local data  = cjson.encode(apiReq)
        query = ngx.var.args
  do
    headers['X-Forwarded-For'] = ngx.var.proxy_add_x_forwarded_for
  end

  --重新装填 方法；移除或者指定为特定需要的method
  headers['X-HTTP-Method-Override']=method
    
  -- perform http request
  local resp, err = httpclient.new()
    :uri(location)
    :headers(headers)
    :query(query)
    :body(data)
    :send(method, 2000, nil)

  if err or (not resp) then
    ngx.status = 500
    ngx.say(err)
    ngx.exit(ngx.OK)
  end

  -- response
  ngx.header['Server'] = ngx.var.SERVICE_NAME
  ngx.header['Content-Type'] = resp.headers['Content-Type']
  ngx.status = resp.status
  local rmvLowKey="content-length"

  for k,v in pairs(resp.headers) do
     local low=string.lower(k)
     if low==rmvLowKey then
      resp.headers[k]=nil
     end
  end

  fill_response_header(resp.headers)
  ngx.say(resp.body)
  ngx.exit(ngx.OK)
end





